package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;

import com.sun.corba.se.spi.orbutil.fsm.Action;
import com.sun.corba.se.spi.orbutil.fsm.FSM;
import com.sun.corba.se.spi.orbutil.fsm.Input;

import Model.SearchModel;
import View.SearchView;

public class SearchController {
	private SearchView view;
	private SearchModel model;
	private JInternalFrame iframe;

	public SearchController() {
		iframe = new JInternalFrame();
		view = new SearchView();
		model = new SearchModel();
		view.setModel(model);
		doAction();
		iframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		iframe.add(view);
		iframe.setSize(700, 450);
		iframe.setVisible(true);
	}
	public JInternalFrame getFrame(){
		return iframe;
	}
	private void doAction() {
		loadComboBox();
		search();
		doExit();
	}
	private void search(){
		searchID();
		searchName();
		searchFromLocation();
		searchToLocation();
		searchFromProject();
		searchToProject();
		searchFromDepartment();
		searchFromProject();
	}

	private void doExit() {
		// TODO Auto-generated method stub
		view.doExit(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				iframe.dispose();
			}
		});
	}

	private void loadComboBox() {
		try {
			view.loadDepartment();
			view.loadProject();
			view.loadLocation();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void searchFromProject() {
		// TODO Auto-generated method stub
		view.searchFromProject(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					model.searchTransfer(view.getID(), view.getName(),
							view.getFromLocation(), view.getToLocation(),
							view.getFromProject(), view.getToProject(),
							view.getFromDepartment(), view.getToDepartment());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

	}
	private void searchToProject() {
		// TODO Auto-generated method stub
		view.searchToProject(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					model.searchTransfer(view.getID(), view.getName(),
							view.getFromLocation(), view.getToLocation(),
							view.getFromProject(), view.getToProject(),
							view.getFromDepartment(), view.getToDepartment());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}


	private void searchFromLocation() {
		// TODO Auto-generated method stub
		view.searchFromLocation(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					model.searchTransfer(view.getID(), view.getName(),
							view.getFromLocation(), view.getToLocation(),
							view.getFromProject(), view.getToProject(),
							view.getFromDepartment(), view.getToDepartment());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}
	private void searchToLocation() {
		// TODO Auto-generated method stub
		view.searchToLocation(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					model.searchTransfer(view.getID(), view.getName(),
							view.getFromLocation(), view.getToLocation(),
							view.getFromProject(), view.getToProject(),
							view.getFromDepartment(), view.getToDepartment());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}
	private void searchFromDepartment() {
		// TODO Auto-generated method stub
		view.searchFromDepartment(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					model.searchTransfer(view.getID(), view.getName(),
							view.getFromLocation(), view.getToLocation(),
							view.getFromProject(), view.getToProject(),
							view.getFromDepartment(), view.getToDepartment());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}private void searchToDepartment() {
		// TODO Auto-generated method stub
		view.searchToDepartment(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					model.searchTransfer(view.getID(), view.getName(),
							view.getFromLocation(), view.getToLocation(),
							view.getFromProject(), view.getToProject(),
							view.getFromDepartment(), view.getToDepartment());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}



	private void searchName() {
		// TODO Auto-generated method stub
		view.searchName(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					try {
						model.searchTransfer(view.getID(), view.getName(),
								view.getFromLocation(), view.getToLocation(),
								view.getFromProject(), view.getToProject(),
								view.getFromDepartment(), view.getToDepartment());
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
					
			}
		});
	}

	private void searchID() {
		// TODO Auto-generated method stub
		view.searchID(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				if(e.getKeyChar() == KeyEvent.VK_ENTER) {
					try {
						view.setName(model.getNameByID(view.getID()));
						model.searchTransfer(view.getID(), view.getName(),
								view.getFromLocation(), view.getToLocation(),
								view.getFromProject(), view.getToProject(),
								view.getFromDepartment(), view.getToDepartment());
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
	}

	public static void main(String[] agrs) {
		new SearchController();
	}

}
